package com.turman.networkdemo

import android.util.Log
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 * 网络重连拦截器
 */
class RetryInterceptor(private val maxRetry:Int=0):Interceptor {
    private var retriedNum=0 //当前重试次数
    override fun intercept(chain: Interceptor.Chain): Response {
        val request:Request = chain.request()
        Log.i("RetryInterceptor","interceptor 14行：当前retriedNum=$retriedNum")
        var response:Response = chain.proceed(request)
        while (!response.isSuccessful && retriedNum<maxRetry) {
            retriedNum++
            Log.i("RetryInterceptor","interceptor 19行：当前retriedNum=$retriedNum")
            response = chain.proceed(request)
        }
        return response
    }
}